<?php
/**
 * (model) 移库订单
 */
namespace App\Models\OverSeasIn;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\OverSeasBa\Warehouse;
use App\Models\Product\ProductSku;
use App\Models\Basics\SysPlatAccount;
use Illuminate\Database\Eloquent\Model;
class RemovaleOrder extends Model
{
    protected $table = 'os_in_removale_order';

    protected $guarded = [];

    public function detail(){
        return $this->hasMany(RemovaleDetail::class,'removale_order_id','id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function warehouse(){
        return $this->belongsTo(Warehouse::class,'warehouse_id');
    }

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public static $_headersFields = [
        'account_id',
        'order_id',
        'seller_sku',
        'fnsku',
        'warehouse_id',
        'area_id',
        'country_id',
        'psku_code',
        'plan_num',
        'ship_num',
        'carrier',
        'tracking_number',
        'shipment_date',
    ];

    public static function getData($re){
        foreach ($re as $row){
            $account = SysPlatAccount::getAccountById($row->account_id);
            $warehouse = Warehouse::getCodeById($row->warehouse_id);
            $area = $row->area->area ?? '';
            $country = $row->country->country ?? '';
            $skuCode = $row->sku->psku_code ?? '';
            $temp = [];
            $temp['account_id'] = $account;
            $temp['order_id'] = $row->order_id;
            $temp['seller_sku'] = $row->seller_sku;
            $temp['fnsku'] = $row->fnsku;
            $temp['warehouse_id'] = $warehouse;
            $temp['area_id'] = $area;
            $temp['country_id'] = $country;
            $temp['sku'] = $skuCode;
            $temp['plan_num'] = $row->plan_num;
            $temp['ship_num'] = $row->ship_num;
            $temp['carrier'] = '';
            $temp['tracking_number'] = '';
            $temp['shipment_date'] = '';
            yield $temp;
            foreach($row->detail as $detail){
                $temp = [];
                $temp['account_id'] = $account;
                $temp['order_id'] = $row->order_id;
                $temp['seller_sku'] = $row->seller_sku;
                $temp['fnsku'] = $row->fnsku;
                $temp['warehouse_id'] = $warehouse;
                $temp['area_id'] = $area;
                $temp['country_id'] = $country;
                $temp['sku'] = $skuCode;
                $temp['plan_num'] = '';
                $temp['ship_num'] = $detail->ship_num;
                $temp['carrier'] = $detail->carrier;
                $temp['tracking_number'] = $detail->tracking_number;
                $temp['shipment_date'] = $detail->shipment_date;
                yield $temp;
            }
        }
    }
}
